package com.hrzyit.whistle.comm.ui.fragment

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hrzyit.base.ui.fragment.BaseMvpFragment
import com.hrzyit.whistle.comm.R
import com.hrzyit.whistle.comm.common.CommConstant
import com.hrzyit.whistle.comm.data.bean.ContactEntity
import com.hrzyit.whistle.comm.injection.component.DaggerContactCenterComponent
import com.hrzyit.whistle.comm.injection.module.ContactCenterModule
import com.hrzyit.whistle.comm.presenter.ContactsPresenter
import com.hrzyit.whistle.comm.presenter.view.ContactsView
import com.hrzyit.whistle.comm.ui.activity.ContactActivity
import com.hrzyit.whistle.comm.ui.activity.ContactMainActivity
import com.hrzyit.whistle.comm.ui.adapter.ContactsAdapter
import kotlinx.android.synthetic.main.fragment_contacts.*
import me.yokeyword.indexablerv.IndexableLayout
import org.jetbrains.anko.support.v4.startActivity

/**
 * 通讯录：联系人 Fragment
 * @author YangWei
 * @date 2019/4/19
 */
class ContactsFragment : BaseMvpFragment<ContactsPresenter>(), ContactsView {

    // 获取当前选择的单位id
    private var deptId: Long? = null

    private lateinit var indexAdapter: ContactsAdapter

    private lateinit var activity: Activity

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        activity = context as Activity

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_contacts, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initAdapter()

        initData()

    }


    /**
     * 初始化联系人 IndexableLayout
     */
    private fun initAdapter() {
        // 初始化联系人索引选择器
        // 适配器
        val indexLayout = contactsIndexLayout as IndexableLayout
        indexLayout.setLayoutManager(LinearLayoutManager(activity))

        // setAdapter
        indexAdapter = ContactsAdapter(activity)
        indexLayout.setAdapter(indexAdapter)

        // set Material Design OverlayView
        indexLayout.setOverlayStyle_MaterialDesign(Color.BLUE)

        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        indexLayout.setCompareMode(IndexableLayout.MODE_FAST)

        // set Listener
        indexAdapter.setOnItemContentClickListener { _, _, _, entity ->
            // 跳转到查看联系人详情
            startActivity<ContactMainActivity>(
                "fragment_key" to CommConstant.RESULT_CODE_CONTACT_DETAIL, "userId" to entity.id
            )
        }
//        indexAdapter.setOnItemTitleClickListener { _, currentPosition, indexTitle ->
//        }
    }

    /**
     * 加载数据
     */
    private fun initData() {
        // 获取参数：单位id
        deptId = activity.intent.getLongExtra("deptId", 0L)
        mPresenter.selectContactsList(deptId!!)
    }


    /**
     * Dagger2 注入
     */
    override fun injectComponent() {

        DaggerContactCenterComponent.builder()
            .activityComponent(mActivityComponent)
            .contactCenterModule(ContactCenterModule())
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    /**
     * 获取单位人员列表信息回调
     */
    override fun onGetSelectContactsListSuccess(result: MutableList<ContactEntity>) {
        indexAdapter.setDatas(result)
    }
}